package com.syh.jn.scm.domain.downstream.entity.system;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.util.secret.PasswordUtils;
import cn.redscarf.commons.spring.SpringContextUtils;
import com.syh.core.ddd.domain.AbstractSimpleAggregate;
import com.syh.jn.scm.domain.downstream.serivce.DownAccountOfPhone.DownAccountOfPhoneDomainService;
import com.syh.jn.scm.dto.downstream.system.downAccountOfPhone.DownAccountOfPhoneSto;
import com.syh.jn.scm.exception.BusinessException;
import com.syh.jn.scm.service.api.downstream.support.enums.DownstreamEnums;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.util.List;

/**
 * Created by soci.song on 2018/12/13.
 * 服务商手机登录账号管理
 */
@Data
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
public class DownAccountOfPhone extends AbstractSimpleAggregate<Long> {
    /**
     * 登录手机账号
     */
    private String loginPhone;
    /**
     * 登录密码
     */
    private String loginPassword;
    /**
     * 姓名
     */
    private String name;
    /**
     * 使用情况
     */
    private Short status;
    /**
     * 默认登录的关联账号
     */
    private Long defualtAccountId;
    /**
     * 绑定的账号ids
     */
    private List<Long> accountIds;
    /**
     * 绑定的账号
     */
    private List<DownAccount> accounts;
    /**
     *  所属终端系统:DOWNSTREAM,OPERATION,SUPPLIER,UPSTREAM
     */
    private String terminalType;

    public DownAccountOfPhone(DownAccountOfPhoneSto sto){
        //验证绑定账号正确性
        DownAccountOfPhoneDomainService domainService = (DownAccountOfPhoneDomainService) SpringContextUtils.getBean(DownAccountOfPhoneDomainService.class);
        this.accountIds = domainService.verifyAccountPasswords(sto.getAccounts());
        ExtBeanUtils.copyProperties(sto,this);
        this.initDefault();
    }

    @Override
    protected void initDefault() {
        super.initDefault();
        this.terminalType = DownstreamEnums.AccountType.DOWNSTREAM.toString();
        this.status = 1;
        this.defualtAccountId = this.accountIds.get(0);
        this.loginPassword = PasswordUtils.generate(this.loginPassword);
    }

    public void destory() {
        //清除与账号的一对多关系
        DownAccountOfPhoneDomainService domainService = (DownAccountOfPhoneDomainService) SpringContextUtils.getBean(DownAccountOfPhoneDomainService.class);
        domainService.clearCorrelation(this.id);
    }

    public void binding(Long accountId) {
        //清除与账号的一对多关系
        DownAccountOfPhoneDomainService domainService = (DownAccountOfPhoneDomainService) SpringContextUtils.getBean(DownAccountOfPhoneDomainService.class);
        domainService.bindingAccount(this.id,accountId);
    }

    public void untying(Long accountId) {
        if(this.defualtAccountId.equals(accountId))
            throw new BusinessException("请先重新设置一个默认登录账号再解绑该账号！");
        //清除与账号的一对多关系
        DownAccountOfPhoneDomainService domainService = (DownAccountOfPhoneDomainService) SpringContextUtils.getBean(DownAccountOfPhoneDomainService.class);
        domainService.untyingAccount(this.id,accountId);
    }

    public void setDefualtSign(Long accountId) {
        //设置默认登录账号
        this.defualtAccountId = accountId;
    }

    public void enable() {
        this.status = 1;
    }

    public void disable() {
        this.status = -1;
    }

    public boolean checkStatus() {
        boolean alter = true;
        for (DownAccount account : this.accounts) {
            if(this.defualtAccountId.equals(account.getId()))
                alter = false;
        }
        if(alter){
            if(this.accounts.isEmpty()){
                this.defualtAccountId = null;
                this.status = -1;
            }
            else {
                this.defualtAccountId = this.accounts.get(0).getId();
            }
        }
        return alter;
    }
}
